#include <SDL.h>
#include "scr_projectile.h"
#include "scr_math.h"
#include "scr_game_data.h"
#include "scr_game.h"
#include "scr_rect.h"
#include "scr_powerup.h"
#include "scr_texture.h"

SCRProjectile::SCRProjectile(const FX32& X, const FX32& Y, const FX32& Heading,
	const FX32& Speed, const int& Lifetime, const int& Damage,
	const SCRTexturePtr Texture)
	: m_FixedX(X)
	, m_FixedY(Y)
	, m_Lifetime(Lifetime)
	, m_Damage(Damage)
	, m_DirectionX(0)
	, m_DirectionY(0)
	, m_Active(1)
	, SCRBasicSprite(Texture, 16, 16, 8, 8)
{
	m_DirectionX = FXMul(SCRMath::FXSin(Heading), Speed);
	m_DirectionY = FXMul(SCRMath::FXCos(Heading), -Speed);
}

SCRProjectile::~SCRProjectile()
{
}

void SCRProjectile::Update(const int& Delta)
{
	m_Lifetime -= Delta;

	if (m_Lifetime >= 0 && m_Active)
	{
		m_FixedX += m_DirectionX;
		m_FixedY += m_DirectionY;

		if (m_FixedX <= 0 ||
			m_FixedX >= FXIntToFX(SCR_MAP_SIZE_X - 1) ||
			m_FixedY <= 0 ||
			m_FixedY > FXIntToFX(SCR_MAP_SIZE_Y - 1))
		{
			m_Active = 0;
		}

		if (m_Active)
		{
			int X = FXFXToInt(m_FixedX);
			int Y = FXFXToInt(m_FixedY);
			SCRRect ProjectileRect(X - 6, Y - 6, 12, 12);
			SCRGameData* pSharedData = SCRGame::GetInstance()->GetSharedData();
			for (std::vector<int>::iterator Iterator = pSharedData->m_Buckets[X >> 8][Y >> 8].begin();
				Iterator != pSharedData->m_Buckets[X >> 8][Y >> 8].end(); ++Iterator)
			{
				int Index = (*Iterator);
				if (pSharedData->m_EnemiesA[Index].m_Active)
				{
					SCRRect EnemyRect(
						FXFXToInt(pSharedData->m_EnemiesA[Index].m_X) - 12,
						FXFXToInt(pSharedData->m_EnemiesA[Index].m_Y) - 12,
						24, 24);
					if (ProjectileRect.Intersect(EnemyRect))
					{
						pSharedData->m_EnemiesA[Index].m_Life -= m_Damage;
						if (pSharedData->m_EnemiesA[Index].m_Life <= 0)
						{
							pSharedData->m_EnemiesA[Index].m_Active = 0;
							int Random = rand() % 100;
							if (Random > 95 && 0 == pSharedData->m_PowerUpA[pSharedData->m_PowerUpIndex].m_Active)
							{
								pSharedData->m_PowerUpA[pSharedData->m_PowerUpIndex].m_Active = 1;
								int RandomX = rand() % 64;
								int RandomY = rand() % 64;
								int Power = (rand() % 2) + 1;
								pSharedData->m_PowerUpA[pSharedData->m_PowerUpIndex].m_Rect.m_X = RandomX * 32;
								pSharedData->m_PowerUpA[pSharedData->m_PowerUpIndex].m_Rect.m_Y = RandomY * 32;
								pSharedData->m_PowerUpA[pSharedData->m_PowerUpIndex].m_Power = Power;
								pSharedData->m_PowerUpIndex++;
								pSharedData->m_PowerUpIndex %= 3;
							}
							SDL_Rect DestRect = { FXFXToInt(pSharedData->m_EnemiesA[Index].m_X) - 32, 
								FXFXToInt(pSharedData->m_EnemiesA[Index].m_Y) - 32, 64, 64 };
							SDL_Rect SourceRect = { 0, 0, 64, 64 };
							SDL_BlitSurface(pSharedData->m_BloodTexture->GetData(), &SourceRect, 
								pSharedData->m_Background->GetData(), &DestRect);
						}
						m_Active = 0;
					}
				}
			}
		}
	}
	else
	{
		m_Active = 0;
	}
}

void SCRProjectile::Render(const int& Delta)
{
	this->Draw(FXFXToInt(m_FixedX), FXFXToInt(m_FixedY), 0, 0);
}
